﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace remes.XDraw
{
   /// <summary>
   /// Interaction logic for XDrawWindow.xaml
   /// </summary>
   public partial class XDrawWindow : Window
   {
      public static readonly DependencyProperty DrawingProperty = DependencyProperty.Register(
         "Drawing", typeof(XDrawing), typeof(XDrawWindow), new PropertyMetadata(null));

      public static readonly DependencyProperty ToolbarButtonSizeProperty = DependencyProperty.Register(
         "ToolbarButtonSize", typeof(double), typeof(XDrawWindow), new PropertyMetadata(48.0));

      public XDrawWindow()
      {
         InitializeComponent();
      }

      private void Exit_Click(object sender, RoutedEventArgs e)
      {
         Close();
      }

      public XDrawing Drawing
      {
         get { return (XDrawing)GetValue(DrawingProperty); }
         set { SetValue(DrawingProperty, value); }
      }

      public double ToolbarButtonSize
      {
         get { return (double)GetValue(ToolbarButtonSizeProperty); }
         set { SetValue(ToolbarButtonSizeProperty, value); }
      }

      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
         Drawing = new XDrawing(canvas);

         DependencyPropertyDescriptor.FromProperty(XDrawing.ModeProperty, typeof(XDrawing)).AddValueChanged(Drawing, DrawingModeChanged);

         CheckModeButton();
      }

      private void DrawingModeChanged(object sender, EventArgs e)
      {
         CheckModeButton();
      }

      private void CheckModeButton()
      {
         btnSelect.IsChecked = Drawing.Mode == XDrawingModes.Select;
         btnEdit.IsChecked = Drawing.Mode == XDrawingModes.Edit;
         btnLine.IsChecked = Drawing.Mode == XDrawingModes.NewLine;
         btnRect.IsChecked = Drawing.Mode == XDrawingModes.NewRect;
         btnEllipse.IsChecked = Drawing.Mode == XDrawingModes.NewEllipse;
         btnPath.IsChecked = Drawing.Mode == XDrawingModes.NewPath;

         miSelect.IsChecked = Drawing.Mode == XDrawingModes.Select;
         miEdit.IsChecked = Drawing.Mode == XDrawingModes.Edit;
         miNewLine.IsChecked = Drawing.Mode == XDrawingModes.NewLine;
         miNewRect.IsChecked = Drawing.Mode == XDrawingModes.NewRect;
         miNewEllipse.IsChecked = Drawing.Mode == XDrawingModes.NewEllipse;
         miNewPath.IsChecked = Drawing.Mode == XDrawingModes.NewPath;
      }

      private void btnSelect_Click(object sender, RoutedEventArgs e)
      {
         Drawing.Mode = XDrawingModes.Select;
         CheckModeButton();
      }

      private void btnEdit_Click(object sender, RoutedEventArgs e)
      {
         Drawing.Mode = XDrawingModes.Edit;
         CheckModeButton();
      }

      private void btnNewLine_Click(object sender, RoutedEventArgs e)
      {
         Drawing.Mode = XDrawingModes.NewLine;
         CheckModeButton();
      }

      private void btnNewRect_Click(object sender, RoutedEventArgs e)
      {
         Drawing.Mode = XDrawingModes.NewRect;
         CheckModeButton();
      }

      private void btnNewEllipse_Click(object sender, RoutedEventArgs e)
      {
         Drawing.Mode = XDrawingModes.NewEllipse;
         CheckModeButton();
      }

      private void btnNewPath_Click(object sender, RoutedEventArgs e)
      {
         Drawing.Mode = XDrawingModes.NewPath;
         CheckModeButton();
      }

      private void btnLoad_Click(object sender, RoutedEventArgs e)
      {
         var dlg = new LoadDrawingDialog();
         dlg.Owner = this;
         dlg.Drawing = Drawing;
         dlg.ShowDialog();
      }

      private void btnSave_Click(object sender, RoutedEventArgs e)
      {
         var dlg = new SaveDrawingDialog();
         dlg.Owner = this;
         dlg.Drawing = Drawing;
         dlg.ShowDialog();
      }

      private void btnClear_Click(object sender, RoutedEventArgs e)
      {
         Drawing.ClearDrawing();
      }

      private void PathSegmentTypeChange_Click(object sender, RoutedEventArgs e)
      {
         var shape = Drawing.SelectedShape as XDrawingPath;
         if (shape != null && Drawing.SelectedControlPoint != null && Drawing.SelectedControlPoint.Tag is PathSegment)
         {
            Drawing.SelectedControlPoint = shape.ChangeSegmentType(Drawing.SelectedControlPoint, (string)(e.OriginalSource as RadioButton).Tag);
         }
      }

      private void AddNewPathPtBefore_Click(object sender, RoutedEventArgs e)
      {
         var shape = Drawing.SelectedShape as XDrawingPath;
         if (shape != null && Drawing.SelectedControlPoint != null)
         {
            Drawing.SelectedControlPoint = shape.AddSegment(Drawing.SelectedControlPoint.Tag, false);
         }
      }

      private void AddNewPathPtAfter_Click(object sender, RoutedEventArgs e)
      {
         var shape = Drawing.SelectedShape as XDrawingPath;
         if (shape != null && Drawing.SelectedControlPoint != null)
         {
            Drawing.SelectedControlPoint = shape.AddSegment(Drawing.SelectedControlPoint.Tag, true);
         }
      }

      private void btnDeleteShape_Click(object sender, RoutedEventArgs e)
      {
         if (Drawing.SelectedShape != null)
         {
            Drawing.RemoveShape(Drawing.SelectedShape);
         }
      }

      private void Button_Click(object sender, RoutedEventArgs e)
      {
         var shape = Drawing.SelectedShape as XDrawingPath;
         if (shape != null && Drawing.SelectedControlPoint != null && Drawing.SelectedControlPoint.Tag is PathSegment)
         {
            shape.DeleteSegment(Drawing.SelectedControlPoint);
         }
      }

      private void miSmallButtons_Click(object sender, RoutedEventArgs e)
      {
         ToolbarButtonSize = 24.0;
         miSmallButtons.IsChecked = true;
         miLargeButtons.IsChecked = false;
      }

      private void miLargeButtons_Click(object sender, RoutedEventArgs e)
      {
         ToolbarButtonSize = 48.0;
         miSmallButtons.IsChecked = false;
         miLargeButtons.IsChecked = true;
      }

      private void miAbout_Click(object sender, RoutedEventArgs e)
      {
         var aboutDlg = new AboutDialog();
         aboutDlg.Owner = this;
         aboutDlg.ShowDialog();
      }
   }
}
